const publishSchema = require('../models/publish')
const userSchema = require('../models/user')
const dayjs = require('dayjs')

const informationCard = {
    getInfoCard: async (ctx, next) => {
        let { pageNum,userId } = ctx.query
        console.log(userId);
        const pageSize = 5; // 每页记录数
        let result = []
        const skipRecords = (pageNum - 1) * pageSize;
        if (ctx.query.userId) {
            result = await publishSchema.find({ userId: ctx.query.userId }).sort({ timestamp: -1 }).skip(skipRecords).limit(pageSize).exec()
        } else {
            result = await publishSchema.find({}).sort({ timestamp: -1 }).skip(skipRecords).limit(pageSize).exec()
        }
        if (result.length) {
            let data = []
            result=result.filter((item)=>{
                return item.timestamp>=new Date()
            })         
            result.forEach((item, index) => {
                let timestamp = dayjs(item.timestamp).format('YYYY年MM月DD日HH时mm分')
                data.push({
                    _id: item._id,
                    passangerSum: item.passangerSum,
                    timestamp: timestamp,
                    Departure: item.Departure,
                    Destination: item.Destination,
                    message: item.message,
                    userId: item.userId,
                    avator: item.avator,
                    nickName: item.nickName
                })
            })
            ctx.body = {
                code: 200,
                data: data,
                msg: '刷新成功'
            }
        } else {
            ctx.body = {
                code: 201,
                data: [],
                msg: '已经到底啦！'
            }
        }
        return;
    },
    selectInfoCard: async (ctx, next) => {

        const { selectMessage,
            selectType } = ctx.query
        let result = []
        if (selectType == '起点查询') {
            result = await publishSchema.find({ Departure: { $regex: selectMessage } })
        } else {
            result = await publishSchema.find({ Destination: { $regex: selectMessage } })
        }
        if (result.length) {
            let data = []
            result.forEach((item, index) => {
                let timestamp = dayjs(item.timestamp).format('YYYY年MM月DD日HH时mm分')
                data.push({
                    _id: item._id,
                    passangerSum: item.passangerSum,
                    timestamp: timestamp,
                    Departure: item.Departure,
                    Destination: item.Destination,
                    message: item.message,
                    userId: item.userId,
                    avator: item.avator,
                    nickName: item.nickName
                })
            })
            ctx.body = {
                code: 200,
                data: data,
                msg: '刷新成功'
            }
        } else {
            ctx.body = {
                code: 201,
                data: [],

            }
        }

        return;


    },
    deleteInfoCard: async (ctx, next) => {
        const { _id } = ctx.request.body
        let result = null
        if (_id) {
            result = await publishSchema.deleteOne({ _id: _id })
            
        } else {
            result = await publishSchema.deleteMany({})
        }

        if (result.deletedCount) {
            ctx.body = {
                code: 200,
                msg: '删除成功'
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '请稍后再试'
            }
        }

        return;


    }

}
module.exports = informationCard